const CONTEXT = require('../../utils/public_var.js')
exports.sendTextMsg = function (that, app, url) {
  //TODO  Bug
  var that = that
  if (app.globalData.OpenID == "" || app.globalData.OpenID == undefined) {
    wx.showModal({
      title: '',
      content: '请您登录验证',
      confirmText: '登录',
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/login/atLogin/atLogin',
          })
        }
      }
    })
    return
  }
  if (that.data.isFree || that.data.totalCount > 0) {
    console.log("有" + that.data.totalCount + "使用次数")
    var tx = that.data.input_tx
    if (that.data.input_tx != "") {
      var message = {
        "role": "user",
        "content": that.data.input_tx,
        "msgtype": CONTEXT.TEXT
      }
      var list = that.data.MessageList
      list.push(message)
      that.setData({
        MessageList: list,
      })
      that.setData({
        intoViewName: 'endview'
      })
      wx.showLoading({
        title: "加载中"
      })
      console.log(that.data.whoAnswe)
      if (that.data.whoAnswer == 'rag') {
        similerAsk(that, app, url, list)
      } else {
        aiAsk(that, app, url, list)
      }
    } else {

      wx.showToast({
        title: '请输入您的问题',
        icon: "none"
      })
    }
  } else {
    // 没有免费使用次数了
    wx.showModal({
      title: '体验用尽',
      content: '在下养家糊口，\n恳请主公雇佣在下',
      confirmText: "雇佣",
      cancelText: "取消",
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/sita/pay/paybill',
          })
        }
      }
    })
  }
}
exports.initMsgData = function (that, data, app) {
  console.log("app.chatList.textTypeList")

  var chatMsg = app.getMsg(that.data.bar_type)
  console.log(chatMsg)
  if (chatMsg != null && chatMsg != undefined && chatMsg.length > 0) {
    that.setData({
      MessageList: chatMsg
    })
    that.setData({
      intoViewName: 'endview'
    })
  } else {

    var initData = [{
      "role": 'system',
      "content": data,
      "msgtype": CONTEXT.TEXT
    }]

    that.setData({
      MessageList: initData
    })
  }





}

function getPromoteList(list) {

  let slicedItems = null
  // if (list.length == 2) {
  //   slicedItems = list.slice(list.length - 1, list.length);
  // } else if (list.length == 3) {
  //   slicedItems = list.slice(list.length - 2);
  // } else {
  //   slicedItems = list.slice(list.length - 3);
  // }
  // 
  slicedItems = list.slice(list.length - 1, list.length);
  console.log(slicedItems)
  return slicedItems
}

function aiAsk(that, app, url, list) {
  var that = that;
  wx.request({
    url: url,
    header: {
      'content-type': 'application/json',
    },
    method: 'POST',
    data: {
      "prompt": getPromoteList(that.data.MessageList),
      'openid': app.globalData.OpenID
    },
    success(res) {
      wx.hideLoading()
      console.log(res.data)
      if (res.data.statusCode == 200) {
        var sys_message = {
          "role": "system",
          "content": res.data.completeOpenAnswer.gptAnswer,
          "msgtype": CONTEXT.TEXT

        }
        list.push(sys_message)
        that.setData({
          MessageList: list,
          input_tx: "",
          totalCount: res.data.completeOpenAnswer.remainingGPTCount,
        })
        app.setCount(res.data.completeOpenAnswer.remainingGPTCount)

        that.setData({
          intoViewName: 'endview'
        })
        app.saveMsg(that.data.bar_type, list)
        that.initSendUI()


      } else {

      }


    },
    fail(res) {
      wx.hideLoading()
      console.log(res)
    }
  })
}

function similerAsk(that, app, url, list) {
  var that = that;
  console.log(that.data.aiboid )
  console.log(getPromoteList(that.data.MessageList))
  console.log(url)
  wx.request({
    url: url,
    method: "post",
    header: {
      'content-type': 'application/json',
    },
    method: 'POST',
    data: {
      "prompt": getPromoteList(that.data.MessageList),
      'openid': app.globalData.OpenID,
      'aiboid':that.data.aiboid
    },
    success(res) {
      console.log(res)
      wx.hideLoading()
      if (res.statusCode == 200) {
        if (res.data.errorcode == 0) {
          var sys_message = {
            "role": "system",
            "content": res.data.data,
            "msgtype": CONTEXT.TEXT

          }

          list.push(sys_message)
          console.log(list)
          that.setData({
            MessageList: list,
            input_tx: "",

          })
          that.setData({
            intoViewName: 'endview'
          })
          that.initSendUI()


        } else {
          wx.showToast({
            title: res.data.errormsg,
          })
        }
      }else{
        if(res.statusCode ==500){
          wx.showToast({
            title: res.data.errormsg,
            icon:'error'
          })
        }
      }


    },
    fail(res) {
      console.log(res)
      wx.showToast({
        title: "问答异常",
        icon:'error'
      })
    }
  })

}
exports.inputChangetx = function (that, app, e) {
  var that = that
  var text = e.detail.data
  that.setData({
    input_tx: text
  })
}
